import java.util.List;
import java.util.UUID;

import redis.clients.jedis.Jedis;
import redis.clients.jedis.Transaction;

public class MyRunnable implements Runnable {

	String watchkeys = "watchkeys";
	Jedis jedis = new Jedis("192.168.1.241", 6379);

	@Override
	public void run() {
		try {
			jedis.watch(watchkeys);

			String val = jedis.get(watchkeys);
			int valint = Integer.valueOf(val);
			String useridname = "user_"+UUID.randomUUID().toString();
			if (valint <10) {
				Transaction tx = jedis.multi();// 开启事务
				tx.incr("watchkeys");
				List<Object>  list = tx.exec();// 提交事务
				if(list!=null){
					System.out.println(useridname+"抢购成功，当前抢到人数:" + (valint+1));
					/* 具体的插入数据库的操作 */
					
					
					jedis.sadd("setsucc",useridname);
				}else{
					System.out.println(useridname+"抢购失败，请下次再来");
					jedis.sadd("setfail", useridname);
				}
				
				Thread.sleep(500);

			} else {
				System.out.println(useridname+"抢购失败，请下次再来");
				jedis.sadd("setfail", useridname);
				Thread.sleep(500);
				return;
			}

		} catch (Exception e) {
			e.printStackTrace();
		} finally {

		}

	}

}
